﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml;
using EPiServer.Configuration;
using TemplateFoundation.Core;

namespace TemplateFoundation.MetaWeblog
{
    /// <summary>
    /// Presents a Windows Live Writer manifest
    /// </summary>
    public class ManifestHandler : IHttpHandler
    {
        #region IHttpHandler Members

        /// <summary>
        /// Gets a value indicating whether another request can use the <see cref="T:System.Web.IHttpHandler"/> instance.
        /// </summary>
        /// <value></value>
        /// <returns>true if the <see cref="T:System.Web.IHttpHandler"/> instance is reusable; otherwise, false.
        /// </returns>
        public bool IsReusable
        {
            get { return false; }
        }

        /// <summary>
        /// Enables processing of HTTP Web requests by a custom HttpHandler that implements the <see cref="T:System.Web.IHttpHandler"/> interface.
        /// </summary>
        /// <param name="context">An <see cref="T:System.Web.HttpContext"/> object that provides references to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.</param>
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/xml";
            var writer = new XmlTextWriter(context.Response.Output);
            writer.WriteStartElement("manifest");
            writer.WriteStartAttribute("xmlns");
            writer.WriteValue("http://schemas.microsoft.com/wlw/manifest/weblog");
            writer.WriteEndAttribute();
            writer.WriteStartElement("options");
            writer.WriteStartElement("clientType");
            writer.WriteString("Metaweblog");
            writer.WriteEndElement();
            writer.WriteStartElement("supportsCategoriesInline");
            writer.WriteString("Yes");
            writer.WriteEndElement();
            writer.WriteStartElement("supportsNewCategories");
            writer.WriteString("Yes");
            writer.WriteEndElement();
            writer.WriteStartElement("supportsNewCategoriesInline");
            writer.WriteString("Yes");
            writer.WriteEndElement();
            writer.WriteStartElement("supportsMultipleCategories");
            writer.WriteString("Yes");
            writer.WriteEndElement();
            writer.WriteStartElement("supportsHeirarchicalCategories");
            writer.WriteString("Yes");
            writer.WriteEndElement();
            writer.WriteStartElement("supportsHierarchicalCategories");
            writer.WriteString("No");
            writer.WriteEndElement();
            writer.WriteStartElement("supportsKeywords");
            writer.WriteString("Yes");
            writer.WriteEndElement();
            writer.WriteStartElement("supportsScripts");
            writer.WriteString("Yes");
            writer.WriteEndElement();
            writer.WriteStartElement("supportsEmbeds");
            writer.WriteString("Yes");
            writer.WriteEndElement();
            writer.WriteStartElement("supportsAuthor");
            writer.WriteString("Yes");
            writer.WriteEndElement();
            writer.WriteStartElement("supportsPages");
            writer.WriteString("Yes");
            writer.WriteEndElement();
            writer.WriteStartElement("supportsPageParent");
            writer.WriteString("Yes");
            writer.WriteEndElement();
            writer.WriteStartElement("supportsPageOrder");
            writer.WriteString("No");
            writer.WriteEndElement();
            writer.WriteStartElement("supportsExcerpt");
            writer.WriteString("Yes");
            writer.WriteEndElement();
            writer.WriteStartElement("supportsFileUpload");
            writer.WriteString("Yes");
            writer.WriteEndElement();
            writer.WriteStartElement("supportsSlug");
            writer.WriteString("Yes");
            writer.WriteEndElement();
            writer.WriteStartElement("supportsAutoUpdate");
            writer.WriteString("Yes");
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteStartElement("weblog");
            writer.WriteStartElement("serviceName");
            writer.WriteString("EPiServer");
            writer.WriteEndElement();
            writer.WriteStartElement("watermarkImageUrl");
            writer.WriteString(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + "/TemplateFoundation/images/metaweblog-watermark-84.png");
            writer.WriteEndElement();
            writer.WriteStartElement("imageUrl");
            writer.WriteString(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + "/TemplateFoundation/images/metaweblog-icon-16.png");
            writer.WriteEndElement();
            writer.WriteStartElement("homepageLinkText");
            writer.WriteString("Go to website");
            writer.WriteEndElement();
            writer.WriteStartElement("homepageUrl");
            writer.WriteCData(Settings.Instance.SiteUrl.ToString());
            writer.WriteEndElement();
            writer.WriteStartElement("postEditingUrl");
            writer.WriteCData("");
            writer.WriteEndElement();
            writer.WriteStartElement("adminLinkText");
            writer.WriteString("Log in to EPiServer");
            writer.WriteEndElement();
            writer.WriteStartElement("adminUrl");
            writer.WriteCData(string.Format("{0}Edit", Settings.Instance.UIUrl).Replace("~", HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority)));
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteStartElement("buttons");
            writer.WriteEndElement();
            writer.WriteEndElement();
        }

        #endregion
    }
}
